package activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lzp.testsmack.R;

import java.util.ArrayList;
import java.util.List;

import utils.LogUtils;

/**
 * Created by EnmaAi on 2017/4/17.
 */

public class ShowActivity extends AppCompatActivity {
    private TextView tv_title;
    private TextView tv_tab1;
    private TextView tv_tab2;
    private TextView tv_tab3;
    private ListView lv_content;

    List<String> items = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        initActionBar();
        LogUtils.ShowLog_d("setContentView complete!");
        //check permission
        int requestCode = 1;
        int sdkInt = Build.VERSION.SDK_INT;
        if (sdkInt >= 23) {
            int checkSelfPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
            if (checkSelfPermission == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(this, new String[]{}, requestCode);
            } else {
                //run allow
            }
        } else {

        }
        LogUtils.ShowLog_d("sdkVersion = " + sdkInt);
        initView();
        setData();
    }

    /**
     * 初始化界面Ui控件
     */
    private void initView() {
        lv_content = (ListView) findViewById(R.id.lv_content);
        tv_tab1 = (TextView) findViewById(R.id.tv_tab1);
        tv_tab2 = (TextView) findViewById(R.id.tv_tab2);
        tv_tab3 = (TextView) findViewById(R.id.tv_tab3);
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.ShowLog_d("position="+position+" is selected!");
            }
        });
    }

    /**
     * 设置listView数据
     */
    private void setData() {
        items.add("item1");
        items.add("item2");
        items.add("item3");
        items.add("item4");
        items.add("item5");
        items.add("item6");
        MyAdapter mAdapter = new MyAdapter(items);
        lv_content.setAdapter(mAdapter);
    }

    /**
     * 初始化工具栏设置
     */
    private void initActionBar() {
        Toolbar toolBar = (Toolbar) findViewById(R.id.activity_show_toolbar);
        setTitle("hello");
        toolBar.setNavigationIcon(R.mipmap.content_back);
        toolBar.setOnCreateContextMenuListener(this);
        setSupportActionBar(toolBar);
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.show_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.app_bar_item){
            LogUtils.ShowLog_d("toolbar item is selected!");
        }else if(item.getItemId()==R.id.app_bar_search){
            LogUtils.ShowLog_d("toolbar search is selected!");
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
            //run allow
        } else {
            //run disallow
        }
    }

    class MyAdapter extends BaseAdapter {
        List<String> items = null;

        public MyAdapter(List<String> items) {
            this.items = items;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder viewHolder;
            if (convertView != null) {
                viewHolder = (Holder) convertView.getTag();
            } else {
                viewHolder = new Holder();
                convertView = View.inflate(ShowActivity.this, R.layout.activity_show_lv_content_item, null);
                viewHolder.iv_show = (ImageView) convertView.findViewById(R.id.iv_show);
                viewHolder.tv_show = (TextView) convertView.findViewById(R.id.tv_show);
                convertView.setTag(viewHolder);
            }
            viewHolder.iv_show.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
            viewHolder.tv_show.setText(items.get(position));
            return convertView;
        }
    }

    static class Holder {
        TextView tv_show;
        ImageView iv_show;
    }
    //30-11-12-8 732553
}
